package vn.intelsoft.vlogvn;

import java.util.List;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubeStandalonePlayer;

import vn.intelsoft.vlogvn.adapter.ListAdapter;
import vn.intelsoft.vlogvn.adapter.VlogAdapter;
import vn.intelsoft.vlogvn.db.DbAdapter;
import vn.intelsoft.vlogvn.instance.Instance;
import vn.intelsoft.vlogvn.instance.Log;
import vn.intelsoft.vlogvn.instance.Rest;
import vn.intelsoft.vlogvn.instance.Vlog;
import vn.intelsoft.vlogvn.instance.Vlogger;
import vn.intelsoft.vlogvn.view.Slider;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class FavoriteActivity extends SherlockFragmentActivity implements
		OnItemClickListener {

	private RelativeLayout layoutNotConnection, layoutLoading, layoutVlog,
			layoutNoData;
	private List<Vlog> vlogs;
	private ListView listView;
	private String url;
	private boolean isLoad = true;
	private DbAdapter db;
	private VlogAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_from_right,
				R.anim.slide_out_to_left);
		setContentView(R.layout.activity_vlog);
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setBackgroundDrawable(
				new ColorDrawable(Color.parseColor("#353844")));
		init();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		overridePendingTransition(R.anim.slide_in_from_left,
				R.anim.slide_out_to_right);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (isLoad) {
			setData();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("onDestroy");
	}

	private void init() {
		db = new DbAdapter(this);

		getSupportActionBar().setTitle("Yêu thích");

		layoutNotConnection = (RelativeLayout) FavoriteActivity.this
				.findViewById(R.id.layout_not_connection);
		layoutNoData = (RelativeLayout) FavoriteActivity.this
				.findViewById(R.id.layout_no_data);
		layoutVlog = (RelativeLayout) FavoriteActivity.this
				.findViewById(R.id.layout_vlog);
		layoutLoading = (RelativeLayout) FavoriteActivity.this
				.findViewById(R.id.layout_loading);
		listView = (ListView) this.findViewById(R.id.list);
		listView.setOnItemClickListener(this);
		registerForContextMenu(listView);
	}

	private void setData() {
		new getVloggers().execute();
	}

	private class getVloggers extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			layoutLoading.setVisibility(View.VISIBLE);
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			// String response = Rest.getJsonString(url);
			// vlogs = Rest.getVlogFromJSON(response);
			vlogs = db.getAllVlog();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			layoutLoading.setVisibility(View.INVISIBLE);
			if (vlogs.size() > 0) {
				layoutVlog.setVisibility(View.VISIBLE);
				// ListAdapter adapter = new ListAdapter(VlogActivity.this,
				// vloggers);
				mAdapter = new VlogAdapter(FavoriteActivity.this, vlogs);
				listView.setAdapter(mAdapter);
				isLoad = false;
			} else {
				layoutNoData.setVisibility(View.VISIBLE);
			}

		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Vlog vlog = vlogs.get(position);
		Intent it = new Intent(FavoriteActivity.this, PlayerActivity.class);
		it.putExtra("id", vlog.vlogId);
		startActivity(it);

		// youtubePlayer(vlog.vlogId);
	}

	private void youtubePlayer(String id) {
		Intent localIntent = YouTubeStandalonePlayer.createVideoIntent(this,
				"AIzaSyDiUH1TwlOb5AAT35HXEWRIUQIZwg0B7dI", id, 0, true, false);
		if (localIntent != null) {
			if (canResolveIntent(localIntent)) {
				// startActivityForResult(localIntent, 1);
				startActivity(localIntent);
			}
		} else {
			YouTubeInitializationResult.SERVICE_MISSING.getErrorDialog(this, 2)
					.show();
			return;
		}
	}

	private boolean canResolveIntent(Intent paramIntent) {
		List localList = getPackageManager().queryIntentActivities(paramIntent,
				0);
		boolean bool1 = false;
		if (localList != null) {
			boolean bool2 = localList.isEmpty();
			bool1 = false;
			if (!bool2)
				bool1 = true;
		}
		return bool1;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.menu1, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo adapterinfo = (AdapterContextMenuInfo) item
				.getMenuInfo();
		Vlog vlog = vlogs.get(adapterinfo.position);

		switch (item.getItemId()) {
		case R.id.delete:
			int a = db.deleteVlogById(vlog.vlogId);
			if (a == 1) {
				vlogs.remove(adapterinfo.position);
				mAdapter.notifyDataSetChanged();
				Toast.makeText(this, "Đã xóa khỏi mục yêu thích!",
						Toast.LENGTH_SHORT).show();
			}
			if (vlogs.size() <= 0) {
				layoutVlog.setVisibility(View.INVISIBLE);
				layoutNoData.setVisibility(View.VISIBLE);
			}
			break;
		case R.id.share:
			Intent localIntent = new Intent();
			localIntent.setAction("android.intent.action.SEND");
			localIntent.putExtra("android.intent.extra.SUBJECT",
					"Check it out!");
			localIntent.putExtra("android.intent.extra.TEXT",
					"http://www.youtube.com/watch?v=" + vlog.vlogId);
			localIntent.setType("text/plain");
			startActivity(Intent.createChooser(localIntent, "Send to..."));
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(
			com.actionbarsherlock.view.MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

}
